"""
独热编码
    将每个元素转为一个1个若干个0的形式
"""
import numpy as np
import sklearn.preprocessing as sp

raw_sample = np.array([[1,3,2],
                       [7,5,4],
                       [1,8,6],
                       [7,3,9]])
# sparse:是否采用稀疏矩阵
encoder = sp.OneHotEncoder(sparse_output=False,dtype='int32')
encoder.fit(raw_sample)
res = encoder.transform(raw_sample)
# res = encoder.fit_transform(raw_sample)
# 稀疏矩阵，记录那个位置有1
print(res)

# 反转
inv_res = encoder.inverse_transform(res)
print(inv_res)